package com.ndp.fb.fbagent.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.ndp.fb.constant.CommonConstants;
import com.ndp.fb.fbagent.service.AdCampaignGroupFBService;
import com.ndp.fb.model.fbagent.param.APIPriority;
import com.ndp.fb.model.fbagent.param.adcampaigngroups.AdCampaignParam;
import com.ndp.fb.model.fbagent.result.FacebookResult;
import com.ndp.fb.model.fbagent.result.adcampaigngroups.FbAdCampaignGroups;
import com.restfb.Parameter;

import java.util.List;

/**
 * Created by infi on 2015/1/24.
 */
@Service
public class AdCampaignGroupFBServiceImpl extends AbstractFBagentService implements AdCampaignGroupFBService {

    @Override
    public Long getCampaignGroupsSummaryByAccount(String accessToken, Long fbAccountId) {
        AdCampaignParam requestParam = new AdCampaignParam();
        requestParam.setFbAccountId(fbAccountId);
        requestParam.setApiPriorityCode(APIPriority.FetchAdCompaignGroupsCount.getCode());
        List<Parameter> parameterList = adapter(requestParam);
        String uri = "/act_" + fbAccountId + "/campaigns";
        FacebookResult<FbAdCampaignGroups> result =
                fetchFacebookResult(uri, accessToken, parameterList, false,
                        0, CommonConstants.NUM_INT_1, FbAdCampaignGroups.class);
        return result.getSummary().getTotalCount();
    }


    @Override
    public void createCampaignGroup(AdCampaignParam requestParam) {
        Parameter[] parameters = this.adapterArray(requestParam);
        //uri="/act_{ad_account_id}/adcampaign_groups"
        String uri = "act_" + requestParam.getFbAccountId() + "/campaigns";
        makePostRequest(uri, requestParam.getAccessToken(), parameters);
    }

    @Override
    public void updateStats(AdCampaignParam param) {
        String uri = "/" + param.getCampaignId();
        List<Parameter> parameterList = adapter(param);
        makePostRequest(uri, param.getAccessToken(), parameterList.toArray(new Parameter[]{}));
    }

}
